package com.cnt.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cnt.dao.DonationRequestDao;
import com.cnt.dao.DonationsDao;
import com.cnt.dao.PointsRulesDao;
import com.cnt.entity.DonationRequest;
import com.cnt.entity.Donations;
import com.cnt.entity.PointsRules;
import com.cnt.service.DonationRequestService;

import com.cnt.vo.CheckDonationsVo;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class DonationRequestServiceImpl implements DonationRequestService {

    @Autowired
    private DonationRequestDao donationRequestDao;
    @Autowired
    private DonationsDao donationsDao;
    @Autowired
    private PointsRulesDao pointsRulesDao;

    @Override
    public ResultVo<String> agreeRequest(Integer requestId) {
        LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
        luw.eq(0 != requestId, DonationRequest::getRequestId, requestId)
                .set(DonationRequest::getReply, "同意");
        int i = donationRequestDao.update(null, luw);
        if (i != 1) return new ResultVo<>(1, "系统繁忙，请稍后再试", null);
        return new ResultVo<>(0, "通过成功", null);
    }

    @Override
    public ResultVo<String> agreeRequestList(List<Integer> list) {
        if(list.size()==0){
            return new ResultVo<>(1, "驳回失败，传递参数为空", null);
        }
        LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
        luw.in(!list.isEmpty(), DonationRequest::getRequestId, list);
        luw.set(DonationRequest::getReply, "同意");
        donationRequestDao.update(null, luw);
        return new ResultVo<>(0, "通过成功", null);
    }

    @Override
    public ResultVo<String> agreeRequestAndChangeAmount(Integer requestId, double changeAmount) {
        DonationRequest donationRequest = donationRequestDao.selectById(requestId);
        int donationId = donationRequest.getDonationId();
        Donations donations = donationsDao.selectById(donationId);
        donationRequest.setReply("同意，申请金额从" + donations.getDonationAmount() + "元修改到" + changeAmount + "元");
        donationRequestDao.updateById(donationRequest);
        donations.setDonationAmount(changeAmount);
        donationsDao.updateById(donations);
        return new ResultVo<>(0, "通过成功", null);
    }

    @Override
    public ResultVo<String> rejectRequest(Integer requestId) {
        LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
        luw.eq(0 != requestId, DonationRequest::getRequestId, requestId)
                .set(DonationRequest::getReply, "驳回");
        int i = donationRequestDao.update(null, luw);
        if (i != 1) return new ResultVo<>(1, "系统繁忙，请稍后再试", null);
        return new ResultVo<>(0, "驳回成功", null);
    }

    @Override
    public ResultVo<String> rejectRequestList(List<Integer> list) {
        if(list.size()==0){
            return new ResultVo<>(1, "驳回失败，传递参数为空", null);
        }
        LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
        luw.in(!list.isEmpty(), DonationRequest::getRequestId, list);
        luw.set(DonationRequest::getReply, "驳回");
        donationRequestDao.update(null, luw);
        return new ResultVo<>(0, "驳回成功", null);
    }

    @Override
    public ResultVo<List<PointsRules>> getPointsRuleByRequest(Integer requestId) {
        DonationRequest donationRequest = donationRequestDao.selectById(requestId);
        String isPointsRules = donationRequest.getIsPointsRules();
        List<PointsRules> list = new ArrayList<>();
        for (String s : isPointsRules.split(",")) {
            System.out.println(s);
            PointsRules pointsRules = pointsRulesDao.selectById(Integer.parseInt(s));
            list.add(pointsRules);
        }
        return new ResultVo<>(0, "查询成功", list);
    }

    @Override
    public ResultVo<List<PointsRules>> setPointsRuleByDonation(Integer donationId, String isPointsRules) {
        Donations donations = donationsDao.selectById(donationId);
        if (donations == null) {
            return new ResultVo<>(1, "查不到该募捐单", null);
        }
        int pointsSum = 0;
        for (String s : isPointsRules.split(",")) {
            int pointsRulesId = Integer.parseInt(s);
            PointsRules pointsRules = pointsRulesDao.selectById(pointsRulesId);
            Integer pointsValue = pointsRules.getPointsValue();
            pointsSum += pointsValue;
        }
        donations.setMaterialPoints(pointsSum);
        donations.setIsPointsRules(isPointsRules);
        int i = donationsDao.updateById(donations);
        if (i == 1) {
            return new ResultVo<>(0, "提交成功", null);
        }
        return new ResultVo<>(2, "提交失败", null);
    }

    @Override
    public List<CheckDonationsVo> getCheckList(Double minAmount, Double maxAmount, String name) {
        return donationRequestDao.getCheckList(minAmount, maxAmount, name);
    }
}
